package page;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 * Servlet implementation class ProfileServlet
 */
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		RequestDispatcher dis;
		Connection con = null;
		
		Integer membre = (Integer)session.getAttribute("user_id");
		if(membre == null){
			dis = request.getRequestDispatcher("/login.jsp");
			dis.forward(request, response);
			return;
		}
		
		try{
			Context namingContext;
			PreparedStatement pstmt;
			ResultSet rs;
			
			namingContext = new InitialContext();
			DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
			con = dataSource.getConnection();
			
			pstmt = con.prepareStatement("SELECT COUNT(*) AS achats_encours FROM `vente` v WHERE v.note IS NULL AND v.id_acheteur = ?");
			pstmt.setInt(1, membre);
			
			rs = pstmt.executeQuery();
			if(rs.next()) {
				request.setAttribute("achats_encours", rs.getInt("achats_encours"));
				rs.close();
			}
			
			pstmt = con.prepareStatement("SELECT COUNT(*) AS ventes_encours FROM `vente` v, `annonce` a WHERE v.id_annonce = a.id_annonce AND v.note IS NULL AND a.id_vendeur = ?");
			pstmt.setInt(1, membre);
			
			rs = pstmt.executeQuery();
			if(rs.next()) {
				request.setAttribute("ventes_encours", rs.getInt("ventes_encours"));
				rs.close();
			}
			
			pstmt = con.prepareStatement("SELECT COUNT(*) AS souhaits_exauce FROM `souhait` s, `annonce` a WHERE a.id_produit=s.id_produit AND a.stock > 0 AND s.prix >= a.prix AND s.id_membre=?");
			pstmt.setInt(1, membre);
			
			rs = pstmt.executeQuery();
			if(rs.next()) {
				request.setAttribute("souhaits_exauce", rs.getInt("souhaits_exauce"));
				rs.close();
			}
			
			dis = request.getRequestDispatcher("/profile.jsp");
			dis.forward(request, response);
		}catch(NamingException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendRedirect("profile.jsp");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendRedirect("profile.jsp");
		} finally {
			try {
				if(con != null) con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
